package com.lipy.microweb.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderServiceImpl implements OrderService {

    private static final String ORDER = "micro-order";
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "queryOrderByIdFallback",
            commandKey = "queryOrderById",
            groupKey = "groupOrder",
            commandProperties = {
                // 策略有两种 THREAD 使用hystrix线程池 SEMAPHORE 使用当前线程，信号量控制并发数
                @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "100000000"),
//                @HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests" , value = "100")
            },
            threadPoolKey = "queryOrderHystrixPool", // 使用的 线程池key 优先级高与groupkey
            threadPoolProperties = {
                @HystrixProperty(name="coreSize", value = "100")
            }
        )
    @Override
    public String queryOrderById(String orderId) {
        return   restTemplate.getForObject("http://"+ORDER +"/order/queryOrder", String.class);
    }
    public String queryOrderByIdFallback(String orderId) {
        return   "fallback "+orderId;
    }

    @Override
    public void addOrder(String orderId, String orderName) {
        System.out.printf("add order success orderId %s,ordername %s",orderId,orderName);
    }
}
